package com.csust.reflection;

import com.csust.Cat;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Reflection02 {
    @Test
    public void test() throws Exception {
        m1();
        m2();
        m3();
    }



    public void m1(){
        long start1 = System.currentTimeMillis();
        Cat c1 = new Cat("花花");
        for (int i = 0; i < 900000000; i++) {
            c1.hi();
        }
        long end = System.currentTimeMillis();
        long end1 = System.currentTimeMillis();
        System.out.println(end1-start1);

    }
    public void m2() throws Exception{
        long start2 = System.currentTimeMillis();
        Class clazz = Cat.class;
        Cat c2 = (Cat)clazz.newInstance();
        Method method = clazz.getMethod("hi");
        for (int i = 0; i < 900000000; i++) {
            method.invoke(c2);
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2-start2);
    }
    public void m3() throws Exception{
        long start2 = System.currentTimeMillis();
        Class clazz = Cat.class;
        Cat c2 = (Cat)clazz.newInstance();
        Method method = clazz.getMethod("hi");
        method.setAccessible(true);
        for (int i = 0; i < 900000000; i++) {
            method.invoke(c2);
        }
        long end2 = System.currentTimeMillis();
        System.out.println(end2-start2);
    }



}
